package cn.bigfire.wzdj.util;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import cn.bigfire.wzdj.pojo.bo.WZDJPack;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
@Slf4j
public class ReqHexGen {

    public static List<String> sendPackBase64List = new ArrayList<>();

    static {
        //发送登录数据包
        sendPackBase64List.add("AQBAAAMACgADAgAACoAECsADemFSMWZJb0hFMEVMTVZ5TWtDcm1RSFhFYkdnSlp3RTJoQkpzd2NGMGEzeGtUd3FYblBaNHQ5TitQcXVEN0ZUQlJRSDZ3T2ZIWlI2R3pBcVd6bXM1Y1FTcCtqZ09FVGl0TWR2SHVsRXQ5aVpLS2R6SjVpWFlBWUdoMGhxNU4xS3U1THZ2NHRZb1FMU1J4Y2NIanBhcjZ0SkkwNlFraFh2MUkyb000YVcyU2ZocUV2Z3Z2OHRreE1hN1pUL0FHdGtFQndQTmtKMzk0S2ljVHhhK3VOZUVya1ljUjNlakJEakkwclRENXFKVlJnUjlMOGg5bndpT3J4ZmtVUkNnV1FGY0hERFlXcVpwRHVreFR0Y1JUeWQ5dFNoVE8rUG1sd2pvNXNwWjVvT0d1RnNxZjhtK0FXcnZzZnYyS1dkWkUwOGtZb2xzY1oyQ1F3d2k4eUZxbE5LOWdQK3MrVHFNMjREaFJybnhsRTB5d1ptOGx3SVJrRGhFM1BxU0habzl4dVBnbGRaa2s5REQzQTB5VGpNY2FId0wrZFVkRnY2Ykw3V0plQ1NTaHdiTzhwSnJneGQ0R2VvVDdKYzR6K1p5VXpWQxoJYmNfd2VjaGF0IgJ6aCoCMTBKKAoId3hPcGVuSWQSHG9sOXNXN1Jpc2NyUnFXT1Exd1ByUVNQME5LRU0=");
        sendPackBase64List.add("AQBAAAIAHgAAAAAA");//订阅福利信息
        sendPackBase64List.add("AQBAAAQAHQAAAAAA");//每日签到信息
        sendPackBase64List.add("");
        sendPackBase64List.add("");
        sendPackBase64List.add("");
        sendPackBase64List.add("");
        sendPackBase64List.add("");
        sendPackBase64List.add("");
        sendPackBase64List.add("");
        sendPackBase64List.add("");
        sendPackBase64List.add("");
        sendPackBase64List.add("");
        sendPackBase64List.add("");

        sendPackBase64List.add("AQBAAAEAFgACAAAACAE=");//匹配
        //换链接
        sendPackBase64List.add("AQBAAD8A6AMrAAAACNHG+gUSJDZiZDNmMmM0LWY5NjEtNDU4NC1hZWUzLWQyZmI5ZjQ2MGVlZQ==");//1
        sendPackBase64List.add("AQBAAAcA6AMAAAAA");//召唤
        sendPackBase64List.add("");
        sendPackBase64List.add("");
        sendPackBase64List.add("");
        sendPackBase64List.add("");
        sendPackBase64List.add("");

    }

    public static void main(String[] args) {
        for (int i = 0; i < sendPackBase64List.size(); i++) {
            int num = i + 1;
            String base64 = sendPackBase64List.get(i);
            if (StrUtil.isBlank(base64)) {
                continue;
            }
            log.info(num + "-----------------------------------------------------------------------------------------------------------------------");
            log.info(genCode(base64));
            log.info(num + "-----------------------------------------------------------------------------------------------------------------------");
//            log.info();
        }
    }

    public static String tlp =
            "    public static void %s() {\n" +
                    "        //%s\n" +
                    "        Wzdj.%s %s = new Wzdj.%s();\n" +
                    "        %s \n" +
                    "        PackUtil.send(Stores.getInstance().getHomeChannel(),  %s);\n" +
                    "    }";

    public static String genCode(byte[] bytes) {
        List<WZDJPack<?>> packList = PackUtil.decodeBytes(bytes);
        StringBuilder sb = new StringBuilder();
        for (WZDJPack<?> wzdjPack : packList) {
            String code = genSingleCode(wzdjPack);
            sb.append(code).append("\n");
        }
        return sb.toString();
    }
    public static String genCode(String base64) {
        byte[] bytes = Base64.getDecoder().decode(base64);
        return genCode(bytes);
    }

    public static String lineTlp = "%s.set%s(%s);";
    public static String lineStrTlp = "%s.set%s(\"%s\");";
    public static String genSingleCode(WZDJPack<?> wzdjPack ) {
        String name = wzdjPack.getHeader().getName();
        String lowerFirst = StrUtil.lowerFirst(name);
        String comment = wzdjPack.toString();
        String todoStr = "//TODO";

        String tryJson = StrUtil.subAfter(comment, ":", false);
        if (JSONUtil.isTypeJSONObject(tryJson)) {
            try {
                JSONObject jsonObject = JSON.parseObject(tryJson);
                StringBuilder sb = new StringBuilder();
                jsonObject.forEach((key,val)->{
                    String upFirstField = StrUtil.upperFirst(key);
                    if (NumberUtil.isNumber(val.toString())) {
                        String line = String.format(lineTlp, lowerFirst, upFirstField, val);
                        sb.append(line).append("\n");
                    } else {
                        String line = String.format(lineStrTlp, lowerFirst, upFirstField, val);
                        sb.append(line).append("\n");
                    }
                });
                todoStr = sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        return String.format(tlp,
                lowerFirst, //方法名
                comment,//注释部分
                name, lowerFirst, name,//new对象
                todoStr,//todoStr字符串
                lowerFirst);//调用方法
    }




}
